package com.jackchen.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.jackchen.dao.UserGroupDAO;
import com.jackchen.po.Usergroup;

public class UserGroupDAOImpl extends IBaseDAOImpl implements UserGroupDAO{
	
	private static final Logger log = Logger.getLogger(UserGroupDAOImpl.class);

	@Override
	public List<Usergroup> findUserGroupByIds(String[] ids) {
		final StringBuffer hsql = new StringBuffer("From Usergroup where id in (");
		for (String id : ids) {
			hsql.append("'");
			hsql.append(id);
			hsql.append("'");
			hsql.append(",");
		}
		hsql.deleteCharAt(hsql.length() - 1);
		hsql.append(")");
		
		List<Usergroup> resultList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hsql.toString());
				List list = query.list();
				return list;
			}
		});
		return resultList;
	}
	
	@Override
	public int deleteUserGroups(String[] ids) {
		final StringBuffer hsql = new StringBuffer("delete from usergroup where id in (");
		for (String id : ids) {
			hsql.append("'");
			hsql.append(id);
			hsql.append("',");
		}
		hsql.deleteCharAt(hsql.length() - 1);
		hsql.append(")");
		
		int result = this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createSQLQuery(hsql.toString());
				int rows = query.executeUpdate();
				return rows;
			}
		});
		return result;
	}
}